package com.choosefine.it.noplatformmaterial.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.choosefine.it.base.BaseController;
import com.choosefine.it.base.dto.PageDTO;
import com.choosefine.it.common.def.CommonConstant;
import com.choosefine.it.noplatformmaterial.dto.NoplatformDetailDTO;
import com.choosefine.it.noplatformmaterial.dto.NoplatformFilterDTO;
import com.choosefine.it.noplatformmaterial.dto.NoplatformMaterialDTO;
import com.choosefine.it.noplatformmaterial.service.APPNoflatformService;
import com.choosefine.it.noplatformmaterial.vo.MaterialConsumeTypeStoreVO;
import com.choosefine.it.noplatformmaterial.vo.NoplatformDetailByEquipTypeVO;
import com.choosefine.it.noplatformmaterial.vo.NoplatformListByMaterialTypeVO;
import com.github.pagehelper.Page;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@Api(value="APP端非平台物料管理",description="熊冬")
@RequestMapping("/")
public class APPNoflatformMaterialController extends BaseController{
	
	
	@Autowired
	private APPNoflatformService appNoflatformService ;
	
	/**
	 * 
	 * @Description：外部物料—库存种类列表->获取对应班组下的材料、设备、设施种类及数量
	 * @Author：XiongDong
	 * @Create Date：2017年2月24日
	 * @param：
	 * @return：没分页，起始页数和单页显示数量没加
	 */
	@GetMapping(value="materialConsumeTypeStore")
	@ApiOperation(value="外部物料—库存种类列表",response=MaterialConsumeTypeStoreVO.class)
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="query",value="项目编号",name="projectCode",dataType="String",required=true),
		@ApiImplicitParam( paramType="query",value="班组id",name="teamId",dataType="int",required=false)
	})
	public String materialConsumeTypeStore(@RequestParam(name="projectCode") String projectCode,
										   Integer teamId){
		Map<String, Object> param = new HashMap<>();
		List<MaterialConsumeTypeStoreVO> noplatList = new ArrayList<>();
		param.put("projectCode", projectCode);
		param.put("teamId", teamId);
		 noplatList = appNoflatformService.materialTypeStore(param);
		return responseSuccess(noplatList);
	}
	/**
	 * 
	 * @throws Exception 
	 * @Description：外部物料—材料—外部材料—查询（调用商城接口获取品种）->商城接口模糊查询，本接口根据种类code精确查询
	 * @Author：XiongDong
	 * @Create Date：2017年2月25日
	 * @param：
	 * @return：没分页，起始页数和单页显示数量没加
	 */
	@GetMapping(value="materialNoplatformFilter")
	@ApiOperation(value="外部物料—材料(设施，设备)—外部材料(设施，设备)—查询",response=NoplatformListByMaterialTypeVO.class)
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="query",value="项目编号",name="projectCode",dataType="String",required=true),
		@ApiImplicitParam(paramType="query",value="班组id",name="teamId",dataType="Integer",required=false),
		@ApiImplicitParam(paramType="query",value="类型:1为材料;2为设施;3为设备",name="type",dataType="int",required=true),
		@ApiImplicitParam(paramType="query",value="种类code，以逗号隔开",name="equipmentTypes",dataType="String",required=false),
		@ApiImplicitParam(paramType="query",value="起始页",name="pageNum",dataType="int",required=false),
		@ApiImplicitParam(paramType="query",value="页面显示条数",name="pageSize",dataType="int",required=false)
	
	})
	public String materialNoplatformFilter(NoplatformFilterDTO noplatformFilterDTO) throws Exception{
		Map<String, Object> param = new HashMap<>();
		PageDTO page =new PageDTO();
		param.put("projectCode",noplatformFilterDTO.getProjectCode());
		param.put("teamId", noplatformFilterDTO.getTeamId());
		param.put("type", noplatformFilterDTO.getType());
//		param.put("pageNum", noplatformFilterDTO.getPageNum());
//		param.put("pageSize", noplatformFilterDTO.getPageSize());
		if(StringUtils.isEmpty(noplatformFilterDTO.getEquipmentTypes())){
			param.put("equipmentTypeList", null);
		}
		else{
			param.put("equipmentTypeList", noplatformFilterDTO.getEquipmentTypes());
		}
		ArrayList<NoplatformListByMaterialTypeVO> noplatList = appNoflatformService.noplatformFilterByMaterialType(param, page);
		return responseSuccess(noplatList);
	}
	/**
	 * 
	 * @Description： 外部物料—材料—外部材料—物料详情->获取某一种类code的物料信息
	 * @Author：XiongDong
	 * @Create Date：2017年2月27日
	 * @param：
	 * @return：
	 */
	@GetMapping(value="materialNoplatformDetail")
	@ApiOperation(value="外部物料—材料(设施，设备)—外部材料(设施，设备)—物料详情",response=NoplatformDetailByEquipTypeVO.class)
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="query",value="项目编号",name="projectCode",dataType="String",required=true),
		@ApiImplicitParam( paramType="query",value="班组id",name="teamId",dataType="int",required=true),
		@ApiImplicitParam(paramType="query",value="类型:1为材料;2为设施;3为设备",name="type",dataType="int",required=true),
		@ApiImplicitParam(paramType="query",value="种类code",name="equipmentType",dataType="int",required=true),
		@ApiImplicitParam(paramType="query",value="起始页",name="pageNum",dataType="int",required=false),
		@ApiImplicitParam(paramType="query",value="页面显示条数",name="pageSize",dataType="int",required=false)
	})
	public String materialNoplatformDetail(NoplatformDetailDTO noplatformDetailDTO){
		PageDTO page = new PageDTO();
		page.setPageNum(noplatformDetailDTO.getPageNum());
		page.setPageSize(noplatformDetailDTO.getPageSize());
		Map<String, Object> param = new HashMap<>();
		param.put("projectCode", noplatformDetailDTO.getProjectCode());
		param.put("teamId", noplatformDetailDTO.getTeamId());
		param.put("type", noplatformDetailDTO.getType());
		param.put("equipmentType", noplatformDetailDTO.getEquipmentType());
		param.put("latestTime", noplatformDetailDTO.getLatestTime());
		if(param.get("latestTime")!=null && param.get("latestTime")!="" ){
			try{//判断日期格式是否正确，正确格式为：yyyyMM
				SimpleDateFormat format=new SimpleDateFormat("yyyyMM");
				format.parse((String) param.get("latestTime"));
			}catch(ParseException  e){
				return responseFail(CommonConstant.MSG_DATE_FORMAT_ERROR);
			}
		}	
		Page<NoplatformDetailByEquipTypeVO> noplatformDetail = appNoflatformService.noplatformDetailByEquipmentType(param,page);
		return responseSuccess(noplatformDetail);
	}
}
